<template>
  <div class="app-container">
    <!-- 高级查询 -->
    <el-form v-show="searchVisible" ref="queryForm" :model="queryParams" :inline="true">
      <el-form-item label="店铺名称:">
        <el-input v-model="queryParams.storeName" size="small" placeholder="请输入店铺名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="店铺编号:">
        <el-input v-model="queryParams.storeCode" size="small" placeholder="请输入店铺编号" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="商城编号:">
        <el-select v-model="queryParams.mallId" placeholder="请选择商城编号" clearable size="small">
          <el-option v-for="option in options.mallIdOptions" :key="option.id" :label="option.customerName" :value="option.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="供应商编号:">
        <el-select v-model="queryParams.supplierId" placeholder="请选择供应商编号" clearable size="small">
          <el-option v-for="option in options.supplierIdOptions" :key="option.id" :label="option.supplierName" :value="option.id" />
        </el-select>
      </el-form-item>
      <el-form-item label="商城名称:">
        <el-input v-model="queryParams.mallName" size="small" placeholder="请输入商城名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item label="供应商名称:">
        <el-input v-model="queryParams.supplierName" size="small" placeholder="请输入供应商名称" clearable @keyup.enter.native="handleQuery" />
      </el-form-item>
      <el-form-item>
        <el-button type="cyan" icon="el-icon-search" size="mini" @click="handleQuery">查询</el-button>
        <el-button icon="el-icon-refresh" size="mini" @click="resetQuery">重置</el-button>
      </el-form-item>
    </el-form>

    <!-- 工具栏 -->
    <el-row :gutter="10" class="mb8">
      <el-col :span="1.5">
        <el-button v-hasPermission="['basic:store:insert']" type="primary" icon="el-icon-plus" size="mini" @click="handleAdd">新增</el-button>
      </el-col>
     <!-- <el-col :span="1.5">
        <el-button v-hasPermission="['basic:store:update']" type="success" icon="el-icon-edit" size="mini" :disabled="isSingle" @click="handleEdit">修改</el-button>
      </el-col> -->
      <el-col :span="1.5">
        <el-button v-hasPermission="['basic:store:delete']" type="danger" icon="el-icon-delete" size="mini" :disabled="isMultiple" @click="handleRemove">删除</el-button>
      </el-col>
      <el-col :span="1.5">
        <el-button v-hasPermission="['basic:store:delete']" type="danger" icon="el-icon-delete" size="mini" :disabled="isMultiple" @click="handleLogicalDelete">作废</el-button>
      </el-col>
      <pagination v-show="page.total>0" :total="page.total" :page.sync="page.current" :limit.sync="page.pageSize" @pagination="handleQuery" />

    <!--  <right-toolbar :show-search.sync="searchVisible" @queryTable="handleQuery" />
   --> </el-row>

    <!-- 数据列表 -->
    <el-table v-loading="loading" :data="page.records" highlight-current-row @selection-change="handleSelectionChange">
      <el-table-column type="selection" width="55" align="center" />
      <el-table-column label="店铺编号" align="center" prop="storeCode" />
      <el-table-column label="店铺名称" align="center" prop="storeName" />
      <el-table-column label="商城编号" align="center" prop="customerCode" />
      <el-table-column label="供应商编号" align="center" prop="supplierCode" />
      <el-table-column label="商城名称" align="center" prop="mallName" />
      <el-table-column label="供应商名称" align="center" prop="supplierName" />
      <el-table-column label="数据生成日期" align="center" prop="createTime" width="180">
        <template slot-scope="scope">
          <span>{{ parseTime(scope.row.createTime, '{y}-{m}-{d}') }}</span>
        </template>
      </el-table-column>
      <el-table-column label="" align="center" prop="remark" />
      <el-table-column label="操作" align="center" class-name="small-padding fixed-width">
       <template slot-scope="scope">
        <el-button v-hasPermission="['basic:store:update']" size="mini" type="text" @click="handleEdit(scope.row)">查看</el-button>
        <el-dropdown type="text" split-button>
          <el-dropdown-menu slot="dropdown">
              <el-dropdown-item v-hasPermission="['basic:store:update']" size="mini" type="primary" icon="el-icon-edit"  @click.native="handleEdit(scope.row)">修改</el-dropdown-item>
              <el-dropdown-item v-hasPermission="['basic:store:delete']" size="mini" type="primary" icon="el-icon-delete" @click.native="handleRemove(scope.row)">删除</el-dropdown-item>
            </el-dropdown-menu>
          </el-dropdown>
       </template>
      </el-table-column>
    </el-table>

    <!-- 分页 -->

    <!-- 添加/修改对话框 -->
    <el-dialog :title="storeFormDialog.title" :visible.sync="storeFormDialog.visible" :close-on-click-modal="false" :before-close="resetStoreFormDialog" width="500px" append-to-body>
      <el-form ref="storeForm" :model="storeFormData" :rules="storeFormRules" label-width="80px">
        <el-form-item label="店铺名称" prop="storeName">
          <el-input v-model="storeFormData.storeName" placeholder="请输入店铺名称" />
        </el-form-item>
        <el-form-item label="商城" prop="mallId">
          <el-select v-model="storeFormData.mallId" style="width: 100%" clearable placeholder="请选择商城编号">
            <el-option v-for="option in options.mallIdOptions" :key="option.id" :label="option.customerName" :value="option.id" />
          </el-select>
        </el-form-item>
        <el-form-item label="供应商" prop="supplierId">
          <el-select v-model="storeFormData.supplierId" style="width: 100%" clearable placeholder="请选择供应商编号">
            <el-option v-for="option in options.supplierIdOptions" :key="option.id" :label="option.supplierName" :value="option.id" />
          </el-select>
        </el-form-item>
       <!-- <el-form-item label="商城名称" prop="mallName">
          <el-input v-model="storeFormData.mallName" placeholder="请输入商城名称" />
        </el-form-item>
        <el-form-item label="供应商名称" prop="supplierName">
          <el-input v-model="storeFormData.supplierName" placeholder="请输入供应商名称" />
        </el-form-item> -->
        <el-form-item label="" prop="remark">
          <el-input v-model="storeFormData.remark" placeholder="请输入" />
        </el-form-item>
      </el-form>
      <div slot="footer" class="dialog-footer">
        <el-button type="primary" @click="confirmStoreFormDialog">确 定</el-button>
        <el-button @click="resetStoreFormDialog">取 消</el-button>
      </div>
    </el-dialog>
  </div>
</template>

<script>
import StoreApi from '@/api/basic/StoreApi'
import CustomerApi from '@/api/basic/CustomerApi'
import SupplierApi from '@/api/basic/SupplierApi'

const defaultStoreFormData = {
  storeName: null,
  mallId: null,
  supplierId: null,
  mallName: null,
  supplierName: null
}
export default {
  name: 'StoreList',
  data() {
    return {
      options: {
        mallIdOptions: [], // 商城编号字典
        supplierIdOptions: [], // 供应商编号字典
        sysYnTypeOptions: this.$constant.sysYnTypeOptions
      },
      queryParams: {
        storeName: null,
        mallId: null,
        supplierId: null,
        mallName: null,
        supplierName: null,
        isValid: this.$constant.Y
      },
      page: {
        current: 1,
        pageSize: 10,
        total: 0,
        records: []
      },
      storeFormDialog: {
        title: '',
        visible: false
      },
      storeFormData: Object.assign({}, defaultStoreFormData),
      storeFormRules: {
      },
      loading: true,
      searchVisible: true,
      multipleSelection: [],
      isSingle: true,
      isMultiple: true
    }
  },
  created() {
    this.handleQuery()
    this.loadOptions()
  },
  methods: {
    loadOptions() {
      CustomerApi.list().then(response => {
        this.options.mallIdOptions = response.data
      })
      SupplierApi.list().then(response => {
        this.options.supplierIdOptions = response.data
      })
    },
    handleAdd() {
      // this.openStoreFormDialog()
      this.$router.push({ path: `/store/form` })
    },
    handleLogicalDelete(row) {
      const ids = row.id || this.multipleSelection.map(item => item.id)
      this.$confirm('是否确认作废数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return StoreApi.logicalDelete(ids)
      }).then(() => {
        this.handleQuery()
        this.$message.success('作废成功')
      })
    },
    handleRemove(row) {
      const ids = row.id || this.multipleSelection.map(item => item.id)
      this.$confirm('是否确认删除数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return StoreApi.remove(ids)
      }).then(() => {
        this.handleQuery()
        this.$message.success('删除成功')
      })
    },
    handleEdit(row) {
      const id = row.id || this.multipleSelection.map(item => item.id)[0]
      // this.openStoreFormDialog(id)
      this.$router.push({ path: `/store/form/${id}` })
    },
    handleQuery() {
      this.loading = true
      StoreApi.page(this.queryParams, this.page).then(response => {
        this.page.records = response.data.records
        this.page.total = response.data.total
        this.loading = false
      }, () => {
        this.loading = false
      })
    },
    handleExport() {
      this.$confirm('是否确认导出数据?', '警告', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      }).then(function() {
        return StoreApi.exportData(this.queryParams)
      }).then(response => {
        this.$common.download(response.data)
      })
    },
    openStoreFormDialog(id) {
      this.storeFormDialog.visible = true
      if (id) {
        this.storeFormDialog.title = '店铺列表-修改'
        StoreApi.get(id).then(response => {
          this.storeFormData = {
            ...this.storeFormData,
            ...response.data
          }
        })
      } else {
        this.storeFormDialog.title = '店铺列表-添加'
      }
    },
    confirmStoreFormDialog() {
      this.$refs['storeForm'].validate(valid => {
        if (valid) {
          if (this.storeFormData.id) {
            StoreApi.edit(this.storeFormData).then(response => {
              this.$message.success('修改成功')
              this.resetStoreFormDialog()
              this.handleQuery()
            })
          } else {
            StoreApi.add(this.storeFormData).then(response => {
              this.$message.success('添加成功')
              this.resetStoreFormDialog()
              this.handleQuery()
            })
          }
        }
      })
    },
    resetStoreFormDialog() {
      this.storeFormDialog = {
        title: '',
        visible: false
      }
      this.storeFormData = Object.assign({}, defaultStoreFormData)
      this.resetForm('storeForm')
    },
    // 商城编号字典
    handleView(row) {
      const id = row.id || this.multipleSelection.map(item => item.id)[0]
      // this.openStoreFormDialog(id)
      this.$router.push({ path: `/store/form/${id}` })
    },
    handleSelectionChange(selection) {
      this.multipleSelection = selection
      this.isSingle = selection.length !== 1
      this.isMultiple = !selection.length
    },
    resetQuery() {
      this.page.current = 1
     // this.resetForm('queryForm')
    this.queryParams = {
      storeCode: null,
      mallId: null,
      supplierId: null,
      mallId: null,
      mallName: null,
      supplierName: null,
      isValid: this.$constant.Y
    }

      this.handleQuery()
    }
  }
}
</script>
